home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: genescot@ix.netcom.com(Eugene S. Thompson )
- Newsgroups: comp.lang.c++
- Subject: External access to private class variables
- Date: 2 Feb 1996 00:45:29 GMT
- Organization: Netcom
- Message-ID: <4ermr9$ii7@cloner2.ix.netcom.com>
- NNTP-Posting-Host: ix-sea7-16.ix.netcom.com
- X-NETCOM-Date: Thu Feb 01 4:45:29 PM PST 1996
-
- I was playing around with references and ran across this situation. I'm
- sure it's not original, so I was hoping someone could tell me if it is
- an intentional implementation of C++ or if it is a bug.
-
- class X
- {
- private:
- int value;
- public:
- X (int n = 0) { value = n; }
- int& GSValue () { return value; } // return a reference to the
- // private member "value"
- };
- .
- .
- .
- main ()
- {
- X x1 (20);
-
- printf ("%d\n", x1.GSValue ()); // 20
-
- int* pntr = &(x1.GSValue ());
-
- printf ("%d\n", *pntr); // 20
-
- *pntr = 30;
- printf ("%d\n", x1.GSValue ()); // 30 -> We now have external
- // access to a private member.
- }
-
- I realize that this implementation is contrived, but I'm sure there are
- additional ways of gaining such access. So, what's the deal?
-
- Gene
-
-
-